package odgs.app.client;

import java.text.NumberFormat;

import org.jfree.data.general.DefaultPieDataset;

import odgs.Connection;
import odgs.Explorer;
import odgs.GridState;
import odgs.app.Util;
import odgs.listener.DataManagerListener;
import odgs.manager.DataManager;

public class GridSystemStateViewController implements DataManagerListener {

	private GridSystemStateViewUI ui;
	private DataManager manager;
	private DefaultPieDataset data;
	
	public GridSystemStateViewController(GridSystemStateViewUI ui) {
		this.ui = ui;
		data = ui.getData();
	}

	public void setDataManager(DataManager manager) {
		this.manager = manager;
		manager.addDataManagerListener(this);
		updateData();
	}

	private void updateData() {
		GridState state = manager.getGridState();
		data.setValue("Used", state.getUsedSize());
		data.setValue("Free", state.getNowCanUseSize());
		data.setValue("Can't use", state.getTotalSize() - state.getUsedSize()- state.getNowCanUseSize());


		long notuse = state.getTotalSize() - state.getUsedSize() - state.getNowCanUseSize();
		NumberFormat nf = NumberFormat.getInstance();
		
		ui.canuse.setText(nf.format(state.getNowCanUseSize())+ " Byte");
		ui.canuse2.setText(Util.getSizeFormat(state.getNowCanUseSize()));
		ui.used.setText(nf.format(state.getUsedSize()) + " Byte");
		ui.used2.setText(Util.getSizeFormat(state.getUsedSize()));
		ui.notuse.setText(nf.format(notuse) + " Byte");
		ui.notuse2.setText(Util.getSizeFormat(notuse));
		ui.total.setText(nf.format(state.getTotalSize())+ " Byte");
		ui.total2.setText(Util.getSizeFormat(state.getTotalSize()));
	}

	public void stateChanged(GridState state) {
		updateData();
	}
	
	public void error(int error) {}
	public void explorerChanged(Explorer explorer){}
	public void joined(GridState state, Explorer explorer, String id) {}
	public void log(String log) {}
	public void logined(GridState state, Explorer explorer) {}
	public void serverConnected(Connection con) {}
}
